﻿using Digitalmes.Scada.Configuration;
using Digitalmes.Scada.Forwarders;
using Digitalmes.Scada.Handlers;
using Digitalmes.Scada.HostedServices;

namespace Digitalmes.Scada;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 注册 Scada 服务。
    /// </summary>
    /// <param name="hostBuilder"></param>
    /// <param name="assemblies">注册 EventBus 的程序集</param>
    public static void AddScada(this IHostBuilder hostBuilder, params Assembly[] assemblies)
    {
        // 注册当前程序集
        List<Assembly> assemblyList =
        [
            typeof(ServiceCollectionExtensions).Assembly,
        ];
        if (assemblies?.Length > 0)
        {
            assemblyList.AddRange(assemblies);
        }

        hostBuilder.AddThingsEdgeRouter()
            .AddDeviceFileProvider()
            .AddNativeForwarder<ScadaNativeForwader>()
            .AddDeviceHeartbeatHandler<HeartbeatApiHandler>()
            .AddDirectMessageRequestHandler<NoticeApiHandler>()
            .AddOpsProvider()
            .AddEventBus([.. assemblyList]);

        hostBuilder.ConfigureServices((context, services) =>
        {
            services.AddScadaServices(context.Configuration);
        });
    }

    private static IServiceCollection AddScadaServices(this IServiceCollection services, IConfiguration configuration)
    {
        services.Configure<ScadaConfig>(configuration.GetSection("Scada"));

        // 注册标记的服务
        services.AddAutoDependencyInjection(typeof(ServiceCollectionExtensions).Assembly);

        // 启动项
        services.AddHostedService<AppStartupHostedService>();

        return services;
    }
}
